package com.ilovetolearn.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.serialization.json.JSON;
	import com.ilovetolearn.business.InitializeDelegate;
	import com.ilovetolearn.model.ModelLocator;
	import com.ilovetolearn.vo.CourseVo;
	import com.ilovetolearn.vo.UserVo;

	import mx.controls.Alert;
	import mx.core.IRepeater;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class InitializeCommand implements ICommand, IResponder
	{
		private var _model:ModelLocator=ModelLocator.getInstance();
		private var delegate:InitializeDelegate;

		public function InitializeCommand()
		{
			delegate=new InitializeDelegate(this);
		}

		public function execute(event:CairngormEvent):void
		{
			delegate.initializeData(event);
		}

		public function result(data:Object):void
		{
			var result:String=String(ResultEvent(data).result);
			trace(result);
			var json:Object=new Object();
			json=JSON.decode(result);
			var resultcode:String=json.resultcode;
			trace("resultcode=" + resultcode);
			var errormsg:String=json.errormsg;
			trace("errormsg=" + errormsg);
			if (resultcode == "0")
			{
				var userinfo:Object=json.userinfo;
				var uid:String=userinfo.uid;
				var nicename:String=userinfo.nicename;
				var avatar:String=userinfo.avatar;
				var gold:String=userinfo.gold;
				if (_model.user == null)
				{
					_model.user=new UserVo();
				}
				_model.user.uid=uid;
				_model.user.nicename=nicename;
				_model.user.avatar=avatar;
				_model.user.gold=gold;
				var course:Object=json.course;
				var courseid:String=course.courseid;
				var coursename:String=course.coursename;
				var coursedesc:String=course.coursedesc;
				var coverurl:String=course.coverurl;
				var level:String=course.level;
				var type:String=course.type;
				var curlang:String=course.curlang;
				var reslang:String=course.reslang;
				var dictationstarttime:String=course.dictationstarttime;
				var dictationendtime:String=course.dictationendtime;
				var dictationtotaltime:String=course.dictationtotaltime;
				var newsentence:String=course.newsentence;
				var finishsentence:String=course.finishsentence;
				var dictationprogress:Number=Math.round(Number(finishsentence) / (Number(newsentence) + Number(finishsentence)) * 100);
				if (_model.course == null)
				{
					_model.course=new CourseVo();
				}
				_model.course.courseid=courseid;
				_model.course.coursename=coursename;
				_model.course.coursedesc=coursedesc;
				_model.course.coverurl=coverurl;
				_model.course.level=level;
				_model.course.type=type;
				_model.course.curlang=curlang;
				_model.course.reslang=reslang;
				_model.course.dictationprogress=dictationprogress.toString();
				_model.course.dictationstarttime=dictationstarttime;
				_model.course.dictationendtime=dictationendtime;
				_model.course.dictationtotaltime=dictationtotaltime;
				_model.curState="DictationMainState";
			}
			else
			{
				Alert.show(errormsg);
			}
		}

		public function fault(info:Object):void
		{
			Alert.show(FaultEvent(info).toString());
		}
	}
}